package com.fcy.teamwork.jmservice

import cn.jpush.android.service.JCommonService
import com.fcy.teamwork.App
import com.fcy.teamwork.repo.RetrofitClient
import retrofit2.http.Body
import retrofit2.http.Header
import retrofit2.http.PUT


class JMService : JCommonService() {
}

/**
{
"text": "",
"target_type": "single",
"from_id": "20195402",
"username": "20101111"
}
 */

data class SingleTextMsg(
    var text: String,
    var username: String,
    var from_id: String = App.person.personId,
    var target_type: String = TYPE_SINGLE,
)

/**
{
text: "20101111",
target_type: "group",
from_id: "20101111",
group_id: 77057275
}
 */
data class GroupTextMsg(
    var text: String,
    var group_id: Long,
    var from_id: String = App.person.personId,
    var target_type: String = TYPE_GROUP
)

const val TYPE_SINGLE = "single"
const val TYPE_GROUP = "group"


interface JMSendService {
    @PUT("v1/im-portal/b2f74964996ed191437d7fed/user/msgsend")
    suspend fun sendMsgSingle(
        @Body body: SingleTextMsg,
        @Header("authorization") authority: String = AUTHORITY_KEY
    ): String

    //    https://bj-api.srv.jpush.cn/v1/im-portal/b2f74964996ed191437d7fed/group/msgsend
    @PUT("v1/im-portal/b2f74964996ed191437d7fed/group/msgsend")
    suspend fun sendMsgGroup(
        @Body body: GroupTextMsg,
        @Header("authorization") authority: String = AUTHORITY_KEY
    ): String

    companion object {
        const val AUTHORITY_KEY =
            "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjdGltZSI6MTY3MjkwNDQwMTMyNywiZXhwIjoxNjcyOTkwODAxMzI3LCJqdGkiOiJhMzk5ZjczYjE1ODY0MTQzOTkwYmIxNzIyN2MxNDZiNiIsIm5hbWUiOiJmMzU1NDI5OTljNjIwYmQzIiwicGFyZW50SWQiOjAsInJlYWRvbmx5IjpmYWxzZSwic3ViIjoiSlB1c2giLCJ0aW1lcyI6MSwidWlkIjo2MDMzMzR9.dhCrebaTb4yrPAQo2KwqLdHsNGobooqboSJOX9fAV8Y"
        private val service: JMSendService by lazy { RetrofitClient.getService("https://bj-api.srv.jpush.cn") }
        suspend fun sendSingle(body: SingleTextMsg) {
            service.sendMsgSingle(body)
        }

        suspend fun sendGroup(body: GroupTextMsg) {
            service.sendMsgGroup(body)
        }
    }
}